<?php


namespace App\Http\Controllers\Api;


use App\Http\Controllers\Controller;
use App\Models\House;
use App\Models\Organize;
use App\Models\OrganizeUsers;
use App\Models\Practice;
use App\Models\PracticeUsers;
use App\Models\Users;
use Illuminate\Http\Request;

class HouseController extends Controller
{

    public function index(Request $request)
    {
        $city_id = $request->get('city_id', 1);
        if ($city_id == 0) {
            $city_id = 1;
        }
        $limit = $request->get('limit', 0);
        $keys = $request->get('keys');
        $model = House::query();
        if ($keys) {
            $model = $model->where('title', 'like', '%' . $keys . '%');
        }
        if ($city_id) {
            $model = $model->where('city_id', $city_id);
        }
        $result = $model->orderBy('id', 'desc')->paginate($limit);
        foreach ($result as $value) {
            if ($value['tags']) {
                $value['tags'] = explode(',', $value['tags']);
            }
            if ($value['drawing']) {
                $value['drawing'] = getAliOssImage($value['drawing']);
            }
            if ($value['project']) {
                $value['project'] = getAliOssImage($value['project']);
            }
        }
        return showMsg('获取成功', 200, $result);
    }

    public function detail(Request $request)
    {
        $id = $request->get('id', 0);
        $result = House::where('id', $id)->first();
        if ($result['tags']) {
            $result['tags'] = explode(',', $result['tags']);
        }
        if ($result['video']) {
            $result['video'] = getAliOssImage($result['video']);
        }
        if ($result['thumb']) {
            $result['thumb'] = getAliOssImage($result['thumb']);
        }
        if ($result['imgs']) {
            $imgs = explode(',', $result['imgs']);
            foreach ($imgs as $key=> $val){
                $imgs[$key] = getAliOssImage($val);
            }
            $result['imgs'] = $imgs;
        } else {
            $result['imgs'] = [];
        }
        if ($result['house']) {
            $house = explode(',', $result['house']);
            foreach ($house as $key=> $val){
                $house[$key] = getAliOssImage($val);
            }
            $result['house'] = $house;
        } else {
            $result['house'] = [];
        }
        if ($result['photos1']) {
            $photos1 = explode(',', $result['photos1']);
            foreach ($photos1 as $key=> $val){
                $photos1[$key] = getAliOssImage($val);
            }
            $result['photos1'] = $photos1;
        } else {
            $result['photos1'] = [];
        }
        if ($result['photos2']) {
            $photos2 = explode(',', $result['photos2']);
            foreach ($photos2 as $key=> $val){
                $photos2[$key] = getAliOssImage($val);
            }
            $result['photos2'] = $photos2;
        } else {
            $result['photos2'] = [];
        }
        if ($result['photos3']) {
            $photos3 = explode(',', $result['photos3']);
            foreach ($photos3 as $key=> $val){
                $photos3[$key] = getAliOssImage($val);
            }
            $result['photos3'] = $photos3;
        } else {
            $result['photos3'] = [];
        }
        if ($result['content']) {
            $result['content'] = replaceOssImgSrc($result['content']);
        }
        return showMsg('获取成功', 200, $result);
    }
}